package cn.hyp.bantang.module.finance.controller.admin.basezcfz;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.hyp.bantang.framework.common.pojo.PageParam;
import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.pojo.CommonResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import static cn.hyp.bantang.framework.common.pojo.CommonResult.success;

import cn.hyp.bantang.framework.excel.core.util.ExcelUtils;

import cn.hyp.bantang.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hyp.bantang.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hyp.bantang.module.finance.controller.admin.basezcfz.vo.*;
import cn.hyp.bantang.module.finance.dal.dataobject.basezcfz.BaseZcfzDO;
import cn.hyp.bantang.module.finance.service.basezcfz.BaseZcfzService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 个股报告期资产负债")
@RestController
@RequestMapping("/finance/base-zcfz")
@Validated
public class BaseZcfzController {

    @Resource
    private BaseZcfzService baseZcfzService;

    @PostMapping("/create")
    @Operation(summary = "创建个股报告期资产负债")
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:create')")
    public CommonResult<Long> createBaseZcfz(@Valid @RequestBody BaseZcfzSaveReqVO createReqVO) {
        return success(baseZcfzService.createBaseZcfz(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新个股报告期资产负债")
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:update')")
    public CommonResult<Boolean> updateBaseZcfz(@Valid @RequestBody BaseZcfzSaveReqVO updateReqVO) {
        baseZcfzService.updateBaseZcfz(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除个股报告期资产负债")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:delete')")
    public CommonResult<Boolean> deleteBaseZcfz(@RequestParam("id") Long id) {
        baseZcfzService.deleteBaseZcfz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得个股报告期资产负债")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:query')")
    public CommonResult<BaseZcfzRespVO> getBaseZcfz(@RequestParam("id") Long id) {
        BaseZcfzDO baseZcfz = baseZcfzService.getBaseZcfz(id);
        return success(BeanUtils.toBean(baseZcfz, BaseZcfzRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得个股报告期资产负债分页")
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:query')")
    public CommonResult<PageResult<BaseZcfzRespVO>> getBaseZcfzPage(@Valid BaseZcfzPageReqVO pageReqVO) {
        PageResult<BaseZcfzDO> pageResult = baseZcfzService.getBaseZcfzPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseZcfzRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出个股报告期资产负债 Excel")
    @PreAuthorize("@ss.hasPermission('finance:base-zcfz:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseZcfzExcel(@Valid BaseZcfzPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseZcfzDO> list = baseZcfzService.getBaseZcfzPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "个股报告期资产负债.xls", "数据", BaseZcfzRespVO.class,
                        BeanUtils.toBean(list, BaseZcfzRespVO.class));
    }

}